import { Router } from "jsr:@oak/oak/router";
import { authMiddleware } from "../middleware/authMiddleware.ts";
import { adminOnly } from "../middleware/adminOnly.ts";
import { getAuth } from "../services/auth.ts";
import type { ApiResponse } from "../shared/types.ts";

const router = new Router();

router.put("/api/admin/users/:id", authMiddleware, adminOnly, async (ctx) => {
  const { id } = ctx.params;
  const body = await ctx.request.body.json();
  const { username, password } = body;
  
  const auth = getAuth();
  const user = await auth.updateUser(id, { username, password });
  
  const response: ApiResponse<{ user: typeof user }> = {
    success: true,
    data: { user }
  };
  
  ctx.response.body = response;
});

export default router; 